app.controller("z-corporation-c", [ '$scope','$rootScope','$z-http','$sce','z-corporation-s','z-edit-menu-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,$filter) {
		$scope.viewModel = {};

		$scope.editModel = {};
		$scope.editModel.corporation={};
		$scope.editModel.title="";
		
		// 获取当天日期
		$scope.viewModel.effectiveDate = new Date();
		// 定义生效时间框内的值，用于检索
		$scope.editModel.changeEffectiveDate =  new Date();
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'corporation'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载数据
		$scope.viewModel.bindData = function(changeEffectiveDate){
			if(changeEffectiveDate == 1){
				$scope.editModel.changeEffectiveDate = $scope.viewModel.effectiveDate;
			}
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getCorporationList({
				fromDate:$scope.viewModel.effectiveDate,
			},
			function(data){
				$scope.viewModel.dataList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};
		
		//新增
		$scope.viewModel.add = function(retroctive){
			$scope.viewModel.load();
			$scope.editModel.title="新增公司信息";
			$scope.editModel.type = true;
			$scope.editModel.corporation={};
			//验证不能为空
			$scope.editForm.corporationCode.$dirty=false;
			$scope.editForm.corporationName.$dirty=false;
		};
		
		//修正
		$scope.viewModel.update=function(corporation){
			$scope.viewModel.load();
			$scope.editModel.title="修改公司信息";
			$scope.editModel.type = false;
			$scope.editModel.corporation=angular.copy(corporation);
		};

		//验证
		$scope.viewModel.requiredForm= function() {
            if (!$scope.editForm.$valid) {
            	if(!$scope.editForm.corporationCode.$dirty){
            		$scope.editForm.corporationCode.$dirty=true;
            	}
            	if(!$scope.editForm.corporationName.$dirty){
            		$scope.editForm.corporationName.$dirty=true;
            	}
                return true;
            }
            return false;
        };
		
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			$scope.editModel.corporation.fromDate = $scope.editModel.changeEffectiveDate;
			if($scope.editModel.type){
				Service.saveCorporation($scope.editModel.corporation,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("新增成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}else{
				Service.updateCorporation($scope.editModel.corporation,
						function(data){
							$scope.viewModel.unLoad();
							$scope.viewModel.bindData();
							$rootScope.alertInfo("修改成功。");
							$scope.viewModel.isLoading = false;
						},function(error) {
							$rootScope.alertError(error.fault.message);
							$scope.viewModel.isLoading = false;
						});
			}
		};
		
		//删除
		$scope.viewModel.del=function(corporation){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteCorporation({
					corporationId:corporation.corporationId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
		
		$scope.viewModel.checkGlobal=function(){
			return false;
		};
		$scope.viewModel.effectiveDateClose=function(){
        	if($scope.viewModel.radioValue==3){
        		
        	}else{
        		$scope.viewModel.effectiveDate=null;
        	}
        }

		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.effectiveDateOpen = false;
		$scope.viewModel.effectiveDateOpnened = false;
		$scope.viewModel.effectiveDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			$scope.viewModel.effectiveDateOpnened = !$scope.viewModel.effectiveDateOpnened;

			
		};
} ]);